﻿
using System;
using System.Collections.Generic;
using System.Text;

using YouHP.Utility;
using YouHP.BLL;
using YouHP.Model;

namespace YouHP.Pages.TreatmentHistory
{
    public class TreatmentAjax : YouHP.Pages.BasePage
    {

        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            if (IsPost == false) HandleGet();
        }

        protected void HandleGet()
        {
#if GENDATA
            PatientRecordInfo prData = new PatientRecordInfo();
            prData.StartDate = Convert.ToDateTime("2006-03-03");
            prData.RecordDate = Convert.ToDateTime("2007-04-04");
            prData.UpdateDate = Convert.ToDateTime("2008-05-20");
            List<TreatmentHistoryInfo> lst = new List<TreatmentHistoryInfo>();
            DateTime dt1 = Convert.ToDateTime("2007-05-16");
            DateTime dt2 = Convert.ToDateTime("2007-05-21");
            Random rnd = new Random();
            for (int i = 0; i < 8; i++)
            {
                TreatmentHistoryInfo info = new TreatmentHistoryInfo();
                info.StartDate = dt1.AddDays(i * 11);
                info.EndDate = dt2.AddDays(i * 11);
                info.DataValue = 15;
                info.UsePurpose = rnd.Next(4);
                info.StopReason = rnd.Next(4);
                lst.Add(info);
            }
            TreatmentInfo trmData = new TreatmentInfo();
            trmData.Name = "维生素";

#else
            int trmid = RequestTools.GetQueryInt("trmid", 0);
            if (trmid <= 0)
            {
                return;
            }

            TreatmentInfo trmData = TreatmentBLL.Instance.GetTreatmentInfo(trmid);
            if (trmData.Id <= 0)
            {
                return;
            }
            
            if (UserData.Id <= 0) return;
            PatientRecordInfo prData = PatientBLL.Instance.GetPatientRecordInfo(UserData.Id);
            if (prData.Id <= 0) return;

            List<TreatmentHistoryInfo> lst = PatientTreatmentBLL.Instance.GetTreatmentHistoryList(prData.Id, trmid);
#endif

            MakeGraphData(prData, trmData,lst);
        }

        private void MakeGraphData(PatientRecordInfo prData, TreatmentInfo trmdata, List<TreatmentHistoryInfo> lst)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("&SingleTreatment=1&\r\n");
            sb.AppendFormat("&StartDate={0:yyyy-MM-dd}&\r\n", prData.StartDate);
            sb.AppendFormat("&DosageDate={0:yyyy-MM-dd}&\r\n", prData.ConfirmDate);
            sb.AppendFormat("&UpdateDate={0:yyyy-MM-dd}&\r\n", prData.UpdateDate);
            
            sb.AppendFormat("&Var={0};{1}/{2};{3}&\r\n",trmdata.Name, trmdata.MeasurementUnits, trmdata.Frequency, lst.Count);

            int cnt = lst.Count - 1;
            for (int i = 0; i <=cnt; i++)
            {
                TreatmentHistoryInfo info = lst[cnt - i];
                sb.AppendFormat("&Data_{0}={1:yyyy-MM-dd};{2};{3}&\r\n",
                    i, 
                    info.StartDate,
                    info.State != Constants.STOPED_USE ? info.UsePurpose : -1,
                    info.DataValue
                    );
            }

            ResponseXML(sb);
        }

        private void ResponseXML(StringBuilder sb)
        {
            System.Web.HttpContext.Current.Response.Clear();
            System.Web.HttpContext.Current.Response.ContentType = "Text/html";
            System.Web.HttpContext.Current.Response.Expires = 0;

            System.Web.HttpContext.Current.Response.Write(sb.ToString());
            System.Web.HttpContext.Current.Response.End();
        }
    }
}